package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateCategory(category);
    }

    /**
     * 分类分页查询
     */
    @Override
    public PageResult pageSelect(CategoryPageQueryDTO query) {
        //开启分页
        PageHelper.startPage(query.getPage(), query.getPageSize());

        //调mapper层
        Page<Category> page =categoryMapper.selectPage(query);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 启用禁用分类
     * 修改分类状态
     */
    @Override
    public void updateStatusById(Long id) {
        categoryMapper.updateStatusById(id);
    }

    /**
     * 新增分类
     * @param category
     */
    @Override
    public void insertCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setStatus(1);
        categoryMapper.insertCategory(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    /**
     * 根据类型查询分类
     */
    @Override
    public List<Category> getType(Integer type) {
        return categoryMapper.selectByType(type);
    }
}
